//
//  WQVisitorView.m
//  sanwuWeiboOC
//
//  Created by 王先强 on 15/12/6.
//  Copyright © 2015年 王先强. All rights reserved.
//

#import "WQVisitorView.h"
#import "UILabel+WQExtension.h"
#import "UIButton+WQExtension.h"




@interface WQVisitorView ()

@property (nonatomic, strong) UIImageView *circleView;
@property (nonatomic, strong) UIImageView *maskCircleView;
@property (nonatomic, strong) UIImageView *homeView;
@property (nonatomic, strong) UILabel *homeLable;
@property (nonatomic, strong) UIButton *registerButton;
@property (nonatomic, strong) UIButton *loginButton;


@end

@implementation WQVisitorView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self setupUI];
    }
    return self;
}
-(void)setupUI{
    
    self.circleView = [[UIImageView alloc] init];
    self.circleView.image = [UIImage imageNamed:@"visitordiscover_feed_image_smallicon"];
    [self.circleView sizeToFit];
    
    
    self.maskCircleView = [[UIImageView alloc] init];
    self.maskCircleView.image = [UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"];
    [self.maskCircleView sizeToFit];
    
    self.homeView = [[UIImageView alloc] init];
    self.homeView.image = [UIImage imageNamed:@"visitordiscover_feed_image_house"];
    [self.homeView sizeToFit];
    
    self.homeLable = [UILabel LabelWithTitle:@"关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"];
    self.registerButton = [UIButton buttonWithBackgroundImage:@"common_button_white_disable" title:@"注册" color: [UIColor blueColor]];
    self.loginButton = [UIButton buttonWithBackgroundImage:@"common_button_white_disable" title:@"登陆" color: [UIColor darkGrayColor]];
    [self setupSubViews];
}

-(void)setupSubViews{
    
    [self addSubview:self.circleView];
    [self addSubview:self.maskCircleView];
    [self addSubview:self.homeView];
    [self addSubview:self.homeLable];
    [self addSubview:self.registerButton];
    [self addSubview:self.loginButton];
    
    for (UIView *view in self.subviews) {
        view.translatesAutoresizingMaskIntoConstraints = NO;
    }
    //circleView
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.circleView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.circleView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:-80]];
    
    //homeView
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.homeView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.circleView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.homeView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.circleView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
    //homeLable
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.homeLable attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.homeLable attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:40]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.homeLable attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeftMargin multiplier:1.0 constant:60]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.homeLable attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRightMargin multiplier:1.0 constant:-60]];
    //registerButton
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:-50]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.homeLable attribute:NSLayoutAttributeBottomMargin multiplier:1.0 constant:30]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:80]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registerButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:35]];
    //registerButton
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:50]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.homeLable attribute:NSLayoutAttributeBottomMargin multiplier:1.0 constant:30]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:80]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:35]];
    
    
    //遮挡罩maskCircleView布局
    /**
     VFL : 可视化格式语言
     
     H 水平方向
     V 垂直方向
     | 边界
     [] 包装控件
     views: 是一个字典 [名字: 控件名] - VFL 字符串中表示控件的字符串
     metrics: 是一个字典 [名字: NSNumber] - VFL 字符串中表示某一个数值
     */
    
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[mask]-0-|" options:0 metrics:nil views:@{@"mask":self.maskCircleView}]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[mask]-(btnHeigth)-[btn]" options:0 metrics:@{@"btnHeigth": @(-35)} views:@{@"mask":self.maskCircleView, @"btn":self.registerButton}]];
    self.backgroundColor = [UIColor colorWithWhite:237.0 / 255 alpha:1];

    [self.registerButton addTarget:self action:@selector(registerButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.loginButton addTarget:self action:@selector(loginButtonDidClicked:) forControlEvents:UIControlEventTouchUpInside];

}

-(void)circleViewStartAnimation{
    CABasicAnimation *baseAni = [CABasicAnimation animation];
    baseAni.keyPath = @"transform.rotation";
    baseAni.toValue = @(M_PI * 2);
    baseAni.repeatCount = MAXFLOAT;
    baseAni.duration = 20;
    baseAni.removedOnCompletion = NO;
    
    [self.circleView.layer addAnimation:baseAni forKey:nil];
}

-(void)setVisitorViewWithTitle:(NSString *)title homeImageName:(NSString *)imageName{
    self.homeLable.text = title;
    if (imageName == nil) {
        [self circleViewStartAnimation];
        return;
    }
    self.circleView.hidden = YES;
    self.homeView.image = [UIImage imageNamed:imageName];
}

-(void)registerButtonDidClicked:(UIButton *)sender{

    if ([self.delegate respondsToSelector:@selector(visitorView:didClickedRegisterButton:)]) {
        [self.delegate visitorView:self didClickedRegisterButton:sender];
    }
}
-(void)loginButtonDidClicked:(UIButton *)sender{

    if ([self.delegate respondsToSelector:@selector(visitorView:didClickedLoginButton:)]) {
        [self.delegate visitorView:self didClickedLoginButton:sender];
    }
}

@end
